#!/bin/bash

git pull
go install tms-server/tms-gin

envParam=$1
if [ ! -n "$envParam" ]; then
	envParam="dev"
fi
portParam=8001
broadcastPort=10001
if [ "$envParam" == "env" ]; then
	portParam=8001
	broadcastPort=10001
elif [ "$envParam" == "prod" ]; then
	portParam=9001
	broadcastPort=11001
fi

echo "stop tms"

tmsPID=`sudo netstat -nplt | grep tms-gin | grep ${portParam} | awk '{print $7}' | awk -F / '{print $1}'`
echo "old tmsPID: ${tmsPID}"
if [ ${tmsPID} ]
then
	echo "kill tmsPID ${tmsPID}"
	kill -9 ${tmsPID}
fi


echo "starting tms in env:${envParam}"
nohup ${GOPATH}/bin/tms-gin ${envParam} >${GOPATH}/src/tms-server/logs/tms.log 2>&1 &
